/**
 * @file eventqueue.c
 * @brief 实现eventqueue 创建，插入，移除功能，并提供对事件的获取查询功能
 * @author ken (kangjiening@openvalley.net)
 * @version 0.1
 * @date 2023-06-06
 *
 * @copyright Copyright (c) 2023  湖南开鸿智谷数字产业发展有限公司
 *
 * @par 修改日志:
 *     @date 2023-06-06
 *     @version 0.2
 *     @author xx
 *     @brief xx
 */

#include "eventqueue.h"
#include "innervent.h"
#define MAX_QUEUE_COUNT 256
/**
 * @brief 创建一个eventqueue
 * @return MQueueId
 * @author ken (kangjiening@openvalley.net)
 * @date 2023-06-08
 */
struct EventQueueId_S* EventQueueCreate(void)
{

    struct EventQueueId_S* eventQueueId = malloc(sizeof(struct EventQueueId_S));
    if(eventQueueId == NULL) return NULL;
    eventQueueId->id =  QUEUE_Create(NULL, sizeof(struct InnerEvent), MAX_QUEUE_COUNT);
    if(eventQueueId->id == NULL)
    {
        Event_free(eventQueueId);
        return NULL;
    }
    eventQueueId->timeWheelId = TimeWheelCreate(uint32_t wheelPower,uint32_t wheelSize,uint32_t interval); //timewheel写好后修改
    if(eventQueueId->timeWheelId == NULL)
    {
        QUEUE_Destroy(eventQueueId->id);
         Event_free(eventQueueId);
        return NULL;
    }
    eventQueueId->subListId = SubEventListInit();
    if(eventQueueId->subListId  == NULL)
    {
         QUEUE_Destroy(eventQueueId->id);
         TimewheelDestory(eventQueueId->timeWheelId);
         Event_free(eventQueueId);
        return NULL;
    }
    return eventQueueId;
}
/**
 * @brief 插入一个innerEvent时间
 * @param  id     队列的id
 * @param  innerEvent     节点
 * @return int
 * @author ken (kangjiening@openvalley.net)
 * @date 2023-06-08
 */
int EventQueueInsert(struct EventQueueId_S* eventQueueId, struct InnerEvent *innerEvent)
{
    if((innerEvent->eventType ==  IMMITEDTYPE) || (innerEvent->eventTpye ==  PUBTYPE ))
        return QUEUE_Put(eventQueueId->id, (struct InnerEvent *)innerEvent, 0, 0);

    if(innerEvent->eventType == CIRCLETYPE > || innerEvent->eventType == OVERTYPE )
        return TimeWheelInsert(eventQueueId->timeWheelId,innerEvent);
    if(innerEvent->eventType == SUBTYPE)
        return SubEventListInsert(eventQueueId->subListId,innerEvent);
}
/**
 * @brief  从队列里面取出一个inner
 * @param  id
 * @param  innerEvent
 * @return int
 * @author ken (kangjiening@openvalley.net)
 * @date 2023-06-08
 */
int EventQueueGettingFromQueue(struct EventQueueId_S* eventQueueId, struct InnerEvent *innerEvent)
{
    uint8_t pri = 0;
    return QUEUE_Pop(eventQueueId->id, (struct InnerEvent *)innerEvent, &pri, 0);
}

int EventQueueGettingFromTimeWheel(struct EventQueueId_S* eventQueueId, struct InnerEvent *innerEvent)
{

}
/**
 * @brief 销毁队列
 * @param  id
 * @return int
 * @author ken (kangjiening@openvalley.net)
 * @date 2023-06-08
 */
int EventQueueDesctory(struct EventQueueId_S* eventQueueId)
{
    QUEUE_Destroy(eventQueueId->id);
    TimewheelDestory(eventQueueId->timeWheelId);
    SubEventListDestroy(eventQueueId->subListId);
    return 0;
}
